package edu.hawaii.duedates.web.page.index;


import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;
import edu.hawaii.duedates.web.page.DueDatesPage;
import edu.hawaii.duedates.web.page.display.Display;

/**
 * The home page for this DueDates-Omaomao application.
 * 
 * @author Ronn Reeves.
 */
public class Index extends DueDatesPage { 
  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /**
   * Creates a new instance of the home page for DueDates-Omaomao.
   */
  public Index() {
    // Create the menu.
    createMenu();

    // Create the login form.
    createForm();
  }

  /**
   * Creates the menu for the DueDates page.
   */
  private void createMenu() {
    this.loginLink.setEnabled(Boolean.FALSE);
    this.loginLink.setVisible(Boolean.TRUE);
    this.loginLink.add(this.loginText);
    add(this.loginLink);

    this.logoutLink.setEnabled(Boolean.TRUE);
    this.logoutLink.setVisible(Boolean.TRUE);

    this.displayLink.setEnabled(Boolean.FALSE);
    this.displayLink.setVisible(Boolean.FALSE);
    add(this.displayLink);
    
    this.alertsLink.setEnabled(Boolean.FALSE);
    this.alertsLink.setVisible(Boolean.FALSE);
    add(this.alertsLink);  
  }

  /**
   * Creates the form for logging in.
   */
  private void createForm() {
    // Create the login form for accessing this user's libraries.
    Form form = new Form("wuxDueDatesForm");

    // Add the form elements.
    form.add(new Label("wuxUserIdLabel", "Username:"));

    String userId = null;
    form.add(new TextField("wuxUserId", new PropertyModel(this, "userId")));
    setUserId(userId);

    form.add(new Label("wuxPasswordLabel", "Password:"));

    String password = null;
    form.add(new PasswordTextField("wuxPassword", new PropertyModel(this, "password")));
    setUserId(password);

    form.add(this.loginButton);

    form.add(new Label("wuxLoginError", getErrorMessage()));

    // Add the form to this page.
    this.add(form);
  }

  /** Login button. */
  private final Button loginButton = new Button("wuxLogin") {
  
    /** Support serialization. */
    private static final long serialVersionUID = 1L;
  
    /** Login to the DueDates application. */
    @Override
    public void onSubmit() {
      setDueDates();

      if (getDueDates() == null) {
        setErrorMessage("Invalid username or password provided.  (case-sensitive)");
        setResponsePage(new Index());
      }
  
      else {
        setErrorMessage("");
        setResponsePage(new Display());
      }
    }
  };
}
